$(function () {

    $("#companyProvince").change(function () {
        $("#companyCity").val("");
        $("#companyCity").html("<option value=''>请选择</option>");
        var pid=$("#companyProvince").val();
        if (pid!="0"){
            _loadcompanycitys(pid);
        }else{
            alert('请选择所在地省份!');
        }
    });
    $("#serviceProvince").change(function () {
        $("#serviceCity").val("");
        $("#serviceCity").html("<option value=''>请选择</option>");
        var pid=$("#serviceProvince").val();
        if (pid!="0"){
            _loadservicecitys(pid);
        }else{
            alert('请选择所在地省份!');
        }
    });

    $("#auditPass").click(function () {
        var serviceId = $("#serviceId").val();
        var qa = "确定要审核通过吗？";
        var approvalNote ="审核通过";
        var approvalResult = 30;
        $.ajax({
            url: "/serviceProvider/checkAuthentication.action?serviceId="+serviceId,
            type: "get",
            dataType: "json",
            success: function (data) {
                if (data.success) {
                    if (confirm(qa)) {
                        var url = "/serviceProvider/auditing.action";
                        var approvalRecordVo = {};
                        approvalRecordVo.businessId = serviceId;
                        approvalRecordVo.approvalNote = approvalNote;
                        approvalRecordVo.approvalResult = approvalResult;
                        $.ajax({
                            url: url,
                            type: "POST",
                            data:JSON.stringify(approvalRecordVo),
                            contentType : 'application/json;charset=utf-8',
                            dataType: "json",
                            success: function (data) {
                                if (data.success) {
                                    window.location.href = '/serviceProvider/listService.action';
                                } else {
                                    alert("审核失败！请联系系统管理员。");
                                }
                            }
                        });
                    }
                } else {
                    alert("该服务商未进行实名认证，不能通过审核。");
                }
            }
        });
    });

    $("#auditFalse").click(function () {
        $("#title").html('否决原因');
        $("#selectrow").val($("#serviceId").val());
        $("#operateStatus").val('50');
        $("#myModalLabel").text("提示");
        $("#popup_message").append("确定要否决吗？");
        $("#popup_message").hide();
        $("#submitbutton").show();
        $("#popup_reason").show();
        $("#myModal").model("show");
    });

    $("#auditBack").click(function () {
        $("#title").html('驳回原因');
        $("#selectrow").val($("#serviceId").val());
        $("#operateStatus").val('40');
        $("#myModalLabel").text("提示");
        $("#popup_message").append("确定要驳回审核吗？");
        $("#popup_message").hide();
        $("#submitbutton").show();
        $("#popup_reason").show();
        $("#myModal").model("show");
    });
})

function _loadcompanycitys(provinceId){
    jQuery.ajax({
        url: "//"+window.location.host+'/address/getCity.action?provinceId='+provinceId,
        type: 'get',
        dataType: 'json',
        success: function(result){
            $("#companyCity").empty();
            $("#companyCity").append("<option value='0'>请选择</option>");
            $.each(result,function(_ind,obj){
                $("#companyCity").append("<option value='"+obj.id+"'>"+obj.name+"</option>");
            });
        }
    });
}

function _loadservicecitys(provinceId){
    jQuery.ajax({
        url: "//"+window.location.host+'/address/getCity.action?provinceId='+provinceId,
        type: 'get',
        dataType: 'json',
        success: function(result){
            $("#serviceCity").empty();
            $("#serviceCity").append("<option value='0'>请选择</option>");
            $.each(result,function(_ind,obj){
                $("#serviceCity").append("<option value='"+obj.id+"'>"+obj.name+"</option>");
            });
        }
    });
}

//弹框跳转
function popupwindow_ok() {
    var serviceId = $('#selectrow').val();
    var pstatus = $('#operateStatus').val();
    var reason = $("#reject_reason").val();
    reason = reason.trim();
    if (reason.length == 0) {
        alert("请填写原因");
        return;
    } else if (reason.length > 30) {
        alert("原因最多30字");
        return;
    }
    auditing(serviceId, pstatus, reason);

}

//审核
function auditing(serviceId,approvalResult,approvalNote){
    var url = "/serviceProvider/auditing.action";
    var approvalRecordVo = {};
    approvalRecordVo.businessId = serviceId;
    approvalRecordVo.approvalNote = approvalNote;
    approvalRecordVo.approvalResult = approvalResult;
    $.ajax({
        url: url,
        type: "POST",
        data:JSON.stringify(approvalRecordVo),
        contentType : 'application/json;charset=utf-8',
        dataType: "json",
        success: function (data) {
            if (data.success) {
                window.location.href = '/serviceProvider/listService.action';
            } else {
                alert("审核失败！请联系系统管理员。");
            }
        }
    });
}
